/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.spring.factory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * a proxy around {@link BeanPostProcessor} that can disable the underlying
 * {@link BeanPostProcessor} in function of a boolean configuration property.
 */
public class RemovableBeanPostProcessor
    implements BeanPostProcessor
{

  /**
   * the {@link BeanPostProcessor} to delegate to.
   */
  private BeanPostProcessor beanPostProcessor;

  /**
   * whether to disable the proxied {@link BeanPostProcessor}.
   */
  private boolean disable = false;



  /**
   * Apply this BeanPostProcessor to the given new bean instance <i>after</i>
   * any bean initialization callbacks (like InitializingBean's
   * <code>afterPropertiesSet</code> or a custom init-method). The bean will
   * already be populated with property values. The returned bean instance may
   * be a wrapper around the original.
   * 
   * @param bean
   *          the new bean instance
   * @param beanName
   *          the name of the bean
   * @return the bean instance to use, either the original or a wrapped one
   * @throws org.springframework.beans.BeansException
   *           in case of errors
   * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object,
   *      java.lang.String)
   */
  public Object postProcessAfterInitialization(Object bean, String beanName)
      throws BeansException
  {
    if (disable) {
      return bean;
    }
    return getBeanPostProcessor().postProcessAfterInitialization(bean, beanName);
  }



  /**
   * Apply this BeanPostProcessor to the given new bean instance <i>before</i>
   * any bean initialization callbacks (like InitializingBean's
   * <code>afterPropertiesSet</code> or a custom init-method). The bean will
   * already be populated with property values. The returned bean instance may
   * be a wrapper around the original.
   * 
   * @param bean
   *          the new bean instance
   * @param beanName
   *          the name of the bean
   * @return the bean instance to use, either the original or a wrapped one
   * @throws org.springframework.beans.BeansException
   *           in case of errors
   * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object,
   *      java.lang.String)
   */
  public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException
  {
    if (disable) {
      return bean;
    }
    return getBeanPostProcessor().postProcessBeforeInitialization(bean, beanName);
  }



  /**
   * getter for the beanPostProcessor property.
   * 
   * @return the beanPostProcessor
   */
  public BeanPostProcessor getBeanPostProcessor()
  {
    return beanPostProcessor;
  }



  /**
   * setter for the beanPostProcessor property.
   * 
   * @param beanPostProcessor
   *          the beanPostProcessor to set
   */
  public void setBeanPostProcessor(BeanPostProcessor beanPostProcessor)
  {
    this.beanPostProcessor = beanPostProcessor;
  }



  /**
   * getter for the disable property.
   * 
   * @return the disable
   */
  public boolean isDisable()
  {
    return disable;
  }



  /**
   * setter for the disable property.
   * 
   * @param disable
   *          the disable to set
   */
  public void setDisable(boolean disable)
  {
    this.disable = disable;
  }

}
